<template>
	<div class="system-role-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入角色名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" plain class="ml10" @click="onOpenAddRole">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增角色
				</el-button>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column type="index" show-overflow-tooltip></el-table-column>
				<el-table-column prop="name" label="角色名称" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="roleSign" label="角色标识" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="status" label="角色状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="sort" label="排序" show-overflow-tooltip></el-table-column>
				<el-table-column prop="remark" label="角色描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="created_at" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpenEditRole(scope.row)"
							>修改</el-button
						>
						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="ruleForm.page"
				background
				v-model:page-size="ruleForm.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<AddRole :menuData="menuData" :apiData="apiData" ref="addRoleRef" @refresh="getRoleLists()" />
		<EditRole :menuData="menuData" :apiData="apiData" ref="editRoleRef" @refresh="getRoleLists()" />
	</div>
</template>

<script setup name="systemRole">
import { ElMessageBox, ElMessage } from 'element-plus';
import AddRole from '@/views/system/role/component/addRole.vue';
import EditRole from '@/views/system/role/component/editRole.vue';

import { getRoleList, delRole } from '@/api/system/role';
import { getMenuList } from '@/api/system/menu';
import { getApiList } from '@/api/system/api';

const menuData = ref([]);
const getMenuLists = async () => {
	let res = await getMenuList();
	if (res.code === 0) {
		menuData.value = res.data;
	}
};
getMenuLists(); //菜单数据
const apiData = ref([]);
const getApiLists = async (type) => {
	let res = await getApiList();
	if (res.code === 0) {
		apiData.value = ListToTree(res.data);
	}
};
getApiLists(); //接口数据

// 列表转树结构
function ListToTree(list) {
	let arrGroup = [];
	for (let i = 0; i < list.length; i++) {
		// 找出每一项的父节点，并将其作为父节点的children
		list[i].type = 1;
		let temp = list[i].group;
		if (!arrGroup.length) {
			arrGroup.push(temp);
		} else {
			let index = arrGroup.findIndex((v) => v.id === temp.id);
			if (index === -1) {
				arrGroup.push(temp);
			}
		}
	}
	arrGroup.forEach((v) => {
		v.type = 0;
		v.children = list.filter((v2) => v2.pid === v.id);
	});
	return arrGroup;
}

const addRoleRef = ref();
const editRoleRef = ref();
const tableData = reactive({
	data: [],
	total: 0,
});
const ruleForm = reactive({
	page: 1,
	limit: 10,
});
// 初始化表格数据
const getRoleLists = async () => {
	let res = await getRoleList(ruleForm);
	if (res.code === 0) {
		// console.log(res.data.data);
		tableData.data = res.data.data;
		tableData.total = res.data.total;
	} else {
		ElMessage.error(res.message);
	}
};
getRoleLists();

// 分页改变
const onHandleSizeChange = () => {
	getRoleLists();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	getRoleLists();
};

// 打开新增角色弹窗
const onOpenAddRole = () => {
	addRoleRef.value.openDialog();
};
// 打开修改角色弹窗
const onOpenEditRole = (row) => {
	editRoleRef.value.openDialog(row);
};
// 删除角色
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await delRole({ id: row.id });
			if (res.code === 0) {
				ElMessage.success('删除成功');
				getRoleLists();
			} else {
				ElMessage.error('删除失败');
			}
		})
		.catch(() => {});
};
</script>
